package com.itheima.dubbo.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
     * 当执行insert时触发执行的方法
     * @param metaObject 实体类对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        Object createed = this.getFieldValByName("createed", metaObject);
        if (createed==null){
            setFieldValByName("created",new Date(),metaObject);

        }
        Object updated = this.getFieldValByName("updated", metaObject);
        if (updated==null){
            setFieldValByName("updated",new Date(),metaObject);

        }

    }
    /**
     * 当执行update操作时触发执行的方法
     * @param metaObject 实体类对象
     */
    @Override
    public void updateFill(MetaObject metaObject) {
      this.setFieldValByName("updated",new Date(),metaObject );
    }
}
